//
//  LYNavigationViewController.m
//  lottery
//
//  Created by 赵国腾 on 16/6/10.
//  Copyright © 2016年 赵国腾. All rights reserved.
//

#import "LYNavigationViewController.h"

@interface LYNavigationViewController ()<UINavigationControllerDelegate>

@property (nonatomic, strong) id popDelegate;

@end

@implementation LYNavigationViewController

+ (void)load {
    
    NSLog(@"%s", __func__);
}

+ (void)initialize {
    
    // 设置导航条的内容 不建议使用，因为该方法会修改系统的导航控制器
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    
    // 获取哪个类下面的导航条
    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    
    [bar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
    
    // 导航条标题颜色
    NSMutableDictionary *titleTextAttributes = [NSMutableDictionary dictionary];
    titleTextAttributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
    titleTextAttributes[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    
    // 该方法会修改自定义导航控制器的样式
    [bar setTitleTextAttributes:titleTextAttributes];
    
    [bar setTintColor:[UIColor whiteColor]];
    
    // 设置返回按钮样式
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -100) forBarMetrics:UIBarMetricsDefault];
    
    NSLog(@"%s", __func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.popDelegate = self.interactivePopGestureRecognizer.delegate;
    
    // 还原滑动返回功能
    self.interactivePopGestureRecognizer.delegate = nil;
    
    self.delegate = self;
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    NSLog(@"%zd", self.viewControllers.count);
    
    // 非根控制器隐藏底部tabBar
    if (self.viewControllers.count != 0) {
        
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 第一种设置返回按钮样式
        // 设置导航控制器左边按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(pop)];
        
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    
    [super pushViewController:viewController animated:animated];
}

//- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
//
//    UIViewController *vc = [super popViewControllerAnimated:animated];
//    
//    NSLog(@"%ld", self.viewControllers.count);
//    
//    if (self.viewControllers.count == 1) {
//        
//        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
//    }
//    
//   return vc;
//}

- (void)pop {
    
    [self popViewControllerAnimated:YES];
    NSLog(@"%s", __func__);
}

#pragma mark - <UINavigationControllerDelegate>

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    
    NSLog(@"----%@", viewController);
    
    if (viewController == self.viewControllers.firstObject) {
        
        self.interactivePopGestureRecognizer.delegate = self.popDelegate;
    }
    
}

@end








